package hms.RoomManagementModule;

import hms.generic.AbstractGlobalView;
import hms.rooms.Room;
import hms.rooms.RoomTable;

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ManageRoomsView extends AbstractGlobalView implements ListSelectionListener{
	
	// The panel to pass to the MasterView
	ManageRoomsPanel panel;
	
	DecommissionPanel dPanel;
	
	// Holds the selected room as an object
	Room selectedRoom = null;
	RoomTable rooms;
	
	/**
	 * The constructor which makes all the calls to different methods and performs logic before making
	 * a call to super.update(panel) and super.display() to display this view. All components should be
	 * added to the panel before calling super.update(panel), and so should all the action listeners
	 * associated with this class.
	 */
	public ManageRoomsView(){
		
		// Create instance of the panel to attach to the view
		panel = new ManageRoomsPanel(this);
		
		// Initialize database connection
		rooms = new RoomTable();

		// Attach and display the view
		super.update(panel);
		super.display();		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		// If make reservation button is pressed
		if(e.getActionCommand().equalsIgnoreCase("btn_makeReservation")){
			clickedButtonMakeReservation();
		}
		
		// If check-in guest button is pressed
		else if(e.getActionCommand().equalsIgnoreCase("btn_checkInGuest")){
			clickedButtonCheckInGuest();
		}
		
		// If more button is pressed
		else if(e.getActionCommand().equalsIgnoreCase("btn_more")){
			clickedButtonMore();
		}
		
		// If decommission button is pressed
		else if(e.getActionCommand().equalsIgnoreCase("btn_decommission")){
			clickedButtonDecommission();
		}
		
		// If reservation status button is pressed
		else if(e.getActionCommand().equalsIgnoreCase("btn_reservationStatus")){
			clickedButtonReservationStatus();
		}
		
		// If Decommission button is pressed
		else if(e.getActionCommand().equalsIgnoreCase("btn_decommissionConfirmation")){
			clickedButtonDecommissionConfirmation();
		}
		
		// If other radio button is selected
		else if(e.getActionCommand().equalsIgnoreCase("radio_other")){
			dPanel.txt_specifyOther.setEnabled(true);
		}
		
		// If non-other radio button is selected
		else if(e.getActionCommand().equalsIgnoreCase("radio_all_except_others")){
			dPanel.txt_specifyOther.setEnabled(false);
		}
		
		// View additional notes from MorePanel
		else if(e.getActionCommand().equalsIgnoreCase("btn_additionalNotes")){
			clickButtonViewAdditionalRoomNotes();
		}
	}
	
	/**
	 * Method to handle displaying the popup with the additional notes for the selected room
	 */
	public void clickButtonViewAdditionalRoomNotes(){
		AdditionalNotesPanel adnPanel = new AdditionalNotesPanel(selectedRoom);
		adnPanel.setVisible(true);
	}
	/**
	 * Method to handle the events for clicking on the decommission confirmation button
	 */
	public void clickedButtonDecommissionConfirmation(){
		
		// Get reason for decommission
		String selectedReason = "";
		if(dPanel.radio_cleaning.isSelected())
			selectedReason = "Cleaning";
		else if(dPanel.radio_maintenance.isSelected())
			selectedReason = "Maintenance";
		else if(dPanel.radio_remodeling.isSelected())
			selectedReason = "Remodeling";
		else if(dPanel.radio_other.isSelected())
			selectedReason = dPanel.txt_specifyOther.getText();

		// If OTHER radio button is selected and no reason is entered
		if(dPanel.radio_other.isSelected() && dPanel.txt_specifyOther.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "You must specify a reason for this action!",
					"Reason not specified", 
					JOptionPane.ERROR_MESSAGE);
		}
		
		// If room is not empty
		else if(!selectedRoom.getOccupationStatus().equalsIgnoreCase("empty")){
			JOptionPane.showMessageDialog(null, "You can only decommission an empty room!",
					"Room not empty", 
					JOptionPane.ERROR_MESSAGE);
		}
		
		else{
			Object[] options = {"Yes", "Cancel"};
				
			int response = JOptionPane.showOptionDialog(dPanel, 
					"This will put the room out of commission until you manually re-commission it. Are you sure you want to do this?",
					"Manual Decommission",
					JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.PLAIN_MESSAGE,
					null,
					options,
					options[0]);
			
			if(response == JOptionPane.YES_OPTION){
				selectedRoom.setDecommissionReason(selectedReason);
				selectedRoom.setOccupationStatus("Out of Commission");
				selectedRoom.commitToDatabase();
				panel.setDecommissionButtonText(selectedRoom);
				panel.lbl_roomStatus.setText("Status: " + selectedRoom.getOccupationStatus());
				
				// CLOSE WINDOW
				dPanel.dispose();
			}
		}

	}
	
	/**
	 * Method to handle the events for clicking on the make reservation button
	 */
	public void clickedButtonMakeReservation(){
		// TODO: [MATT] Add events for this button press
	}
	
	/**
	 * Method to handle the events for clicking on the check-in guest button
	 */
	public void clickedButtonCheckInGuest(){
		// TODO: [MATT] Add events for this button press
	}
	
	/**
	 * Method to handle the events for clicking on the more... button
	 */
	public void clickedButtonMore(){

		// Display popup dialog with more information
		MorePanel more = new MorePanel(selectedRoom, this);
		
		if(selectedRoom.getAdditionalNotes() == null){
			more.btn_additionalNotes.setEnabled(false);
		}
		else
			more.btn_additionalNotes.setEnabled(true);
		
		more.setVisible(true);

	}
	
	/**
	 * Method to handle the events for clicking on the decommission button
	 */
	public void clickedButtonDecommission(){

		// If room is already decommissioned
		if(selectedRoom.getOccupationStatus().equalsIgnoreCase("Out of Commission")){
			Object[] options = {"Yes", "Cancel"};
			
			int response = JOptionPane.showOptionDialog(null, 
					"Are you sure you want to put this room back into commission manually?",
					"Re-Commission",
					JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.PLAIN_MESSAGE,
					null,
					options,
					options[0]);
			
			if(response == JOptionPane.YES_OPTION){
				selectedRoom.setOccupationStatus("Empty");
				selectedRoom.setDecommissionDateComplete(null);
				selectedRoom.setDecommissionReason(null);
				selectedRoom.commitToDatabase();
				
				panel.setDecommissionButtonText(selectedRoom);
				panel.lbl_roomStatus.setText("Status: " + selectedRoom.getOccupationStatus());
			}
		}
		
		// If room is not decommissioned
		else{
			dPanel = new DecommissionPanel(selectedRoom, this, panel);
			dPanel.setVisible(true);
		}
	}
	
	/**
	 * Method to handle the events for clicking on the reservation status button
	 */
	public void clickedButtonReservationStatus(){
		ReservationStatusPanel rPanel = new ReservationStatusPanel(selectedRoom, this);
		rPanel.setVisible(true);
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		

		if (e.getValueIsAdjusting() == false){
			if (panel.list_roomList.getSelectedIndex() != -1){
				// no selection, do something
			}

			updatedSelectedRoom();
			fillInFields();
			
			panel.enableDecommissionButton();
			panel.setDecommissionButtonText(selectedRoom);
			panel.enableMoreButton();
			panel.enableReservationStatusButton();

		}
	}
	
	/**
	 * Assign the chosen room to the selectedRoom object
	 */
	private void updatedSelectedRoom(){
		String selectedRoomLabel = (String) panel.list_roomList.getSelectedValue();
		selectedRoom = rooms.getRoomByLabel(selectedRoomLabel);
	}
	
	/**
	 * Fill in all the fields with the information from the currently selected room
	 */
	private void fillInFields(){
		panel.lbl_roomLabel.setText("Room: " + selectedRoom.getRoomLabel());
		
		panel.lbl_roomStatus.setText("Status: " + selectedRoom.getOccupationStatus());
		
		panel.lbl_roomType.setText("Type: " + selectedRoom.getRoomTypeLabel());
	}

}
